package fr.unice.platdujour.tests;


import static org.junit.Assert.*;

import java.rmi.RemoteException;

import org.junit.BeforeClass;
import org.junit.Test;

import fr.unice.platdujour.application.Network;
import fr.unice.platdujour.chord.Peer;

public class TestPannes {

	/** Number of peers that will be injected in the network */
	private static final int NB_PEERS = 10;

	/** Port number of RMI registry */
	private static final int RMI_REGISTRY_PORT = 1099;

	/** Network **/
	private static Network network;

	@BeforeClass
	public static void initialisation() throws Exception {

		// A tracker is created
		network = Network.getInstance(RMI_REGISTRY_PORT);

	}

	@Test
	public void testNombrePeer() throws RemoteException {
		assertTrue(nbPair() == NB_PEERS);
	}

	/**
	 * Return the peer's number in the network.
	 * 
	 * @return
	 * @throws RemoteException
	 */
	public int nbPair() throws RemoteException {
		Peer depart = network.getTracker().getRandomPeer(), nextPeer = depart;
		int nbPair = 0;

		do {
			nbPair++;
			nextPeer = nextPeer.getSuccessor();
		} while (!depart.equals(nextPeer));

		return nbPair;
	}

}
